package com.donleo.student.controller;


import com.donleo.common.api.CommonResult;
import com.donleo.common.model.Sign;
import com.donleo.common.model.User;
import com.donleo.student.service.SignService;
import com.donleo.student.vo.SignVo;
import com.donleo.student.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 签到表 前端控制器
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/sign")
public class SignController {

    @Autowired
    private SignService signService;

    /**
     * 查询所有学生(包括组长)
     */
    @GetMapping("/findAllStudent")
    public CommonResult findAllStudent(UserVo userVo) {
        List<User> list = signService.findAllStudent(userVo);
        long total = signService.getCountStudent(userVo);
        return CommonResult.success(total, list);
    }

    /**
     * 查询当前登录用户小组成员
     */
    @GetMapping("/findTeamMemberByUserCode")
    public CommonResult findTeamMemberByUserCode(String userCode) {
        List<User> list = signService.findTeamMemberByUserCode(userCode);
        return CommonResult.success(list);
    }

    /**
     * 保存签到数据
     */
    @PostMapping("/saveSignData")
    public CommonResult saveSignData(@RequestBody List<Sign> list) {
        return signService.saveSignData(list);
    }

    /**
     * 查询签到记录
     */
    @GetMapping("/findAllSignRecord")
    public CommonResult findAllSignRecord(UserVo userVo) {
        List<UserVo> list = signService.findAllSignRecord(userVo);
        long total = signService.getCountSignRecord(userVo);
        return CommonResult.success(total, list);
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public CommonResult delete(Integer id) {
        return signService.delete(id);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deletes")
    public CommonResult deletes(Integer[] ids) {
        List<Integer> idsList = Arrays.asList(ids);
        return signService.deletes(idsList);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody Sign sign) {
        Assert.notNull(sign.getId(), "Id is not to be null");
        return signService.updateSign(sign);
    }

    /**
     * 查询单个
     */
    @GetMapping("/findById")
    public CommonResult findById(Integer id) {
        SignVo signVo = signService.findById(id);
        return CommonResult.success(signVo);
    }
}
